//
//  DYVideoPlayerVC.m
//  PrivatePhoto
//
//  Created by PrivatePhoto Team on 2021.
//  Copyright © 2021年 PrivatePhoto Team. All rights reserved.
//

#import "DYVideoPlayerVC.h"
#import "ZQPlayerMaskView.h"
#import <ZQPlayerLandSpaceViewController.h>

@interface DYVideoPlayerVC ()<ZQPlayerDelegate>
/** 视频播放器*/
@property (nonatomic, strong) ZQPlayerMaskView *playerMaskView;
@end

@implementation DYVideoPlayerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSetup];
    [self configSubViews];
}
- (void)initSetup{

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.playerMaskView stopPlay];
    self.playerMaskView.delegate = nil;
    self.playerMaskView = nil;
}

- (void)configSubViews{
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.playerMaskView = [[ZQPlayerMaskView alloc] initWithFrame:CGRectMake(0, self.navigationBarHeight, kScreenWidth, self.view.frame.size.width*0.56)];
    self.playerMaskView.delegate = self;
    self.playerMaskView.isWiFi = YES;
   
  
    [self.view addSubview:self.playerMaskView];
    [self.playerMaskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_equalTo(self.navigationBar.bottom);
        make.height.mas_equalTo(self.view.mas_width).multipliedBy(0.56);
    }];
    @weakify(self)
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self getVideoPathFromPHAsset:self.asset Complete:^(NSString *filePatch) {
            @strongify(self)
//            dispatch_sync(dispatch_get_main_queue(), ^{
                if (filePatch.length) {
                    [self.playerMaskView playWithVideoUrl:filePatch];
                }
                
//            });
           
        }];
//    });
    
}


- (void)getVideoPathFromPHAsset:(PHAsset *)asset Complete:(void (^)(NSString *filePatch))result {
    

    PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
    options.version = PHImageRequestOptionsVersionCurrent;
    options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
    options.networkAccessAllowed = YES;
    [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            AVURLAsset *urlAsset = (AVURLAsset *)asset;
            NSURL *url = urlAsset.URL;
            NSString *filePath = url.absoluteString;
            result(filePath);
            
        });
        
//        AVURLAsset *urlAsset = (AVURLAsset *)asset;
//        NSURL *url = urlAsset.URL;
//        NSString *filePath = url.absoluteString;
//        result(filePath);
    }];
    
}

#pragma mark - 屏幕旋转
//是否自动旋转,返回YES可以自动旋转
- (BOOL)shouldAutorotate {
    return YES;
}
//返回支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}
//这个是返回优先方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}
// 全屏需要重写方法
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator  {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if (orientation == UIDeviceOrientationPortrait || orientation
        == UIDeviceOrientationPortraitUpsideDown) {
        // 隐藏导航栏
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        self.navigationBar.hidden = NO;
        [_playerMaskView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_offset(0);
            make.top.mas_equalTo(self.navigationBar.bottom);
            make.height.mas_equalTo(self.view.mas_width).multipliedBy(0.56);
        }];
    }else {
        // 显示导航栏
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        self.navigationBar.hidden = YES;
        [_playerMaskView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}


- (void)leftBarButtonItemDidClick{
    [self.playerMaskView stopPlay];
    self.playerMaskView.delegate = nil;
    self.playerMaskView = nil;
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark --- setter/getter


@end
